import React from 'react';
import {View, Image, StyleSheet } from 'react-native';
import { Lt, Color, Label, Images, NavTheme as Nav, Jpp, Jx, Touch, Cimage, Fimage, Dialog, Flist, Navigator } from "./_utils";
import { Net, NET_GET } from "./_net"
import { ProjectorLR, eShare } from "./_util"
import { cHome } from './home/_home';
import { ViewMoments } from './ViewMoments';
import { ViewHotDaily } from './ViewHotDaily';

const IDX_HOT = 0, IDX_SPREAD = 1;
// 朋友圈
export class PageMoments extends React.PureComponent{
    constructor(props){
        super(props);
        this.onLoad = this.onLoad.bind(this);
    }
    static navigationOptions = {
        tabBarIcon : ({focused, tintColor})=><Image source={focused ? Images.quan_2 : Images.quan}/>
    }
    // 每日爆款 推广素材
    onLoad(idx, params){
        // ==
        let url = (idx != IDX_HOT) ? "other/dayGoods" : "other/friend";
        Net.request(url, params.pagination, {params}).then(r=>{
            if(r.data){
                // ==
                if(idx != IDX_HOT){
                    params.add(r.data.pageData);
                }else if(r.data.list){
                    for(let k = 0; k < r.data.list.length; ++k){
                        let list = r.data.list[k];
                        let arr = list.dataArray;
                        for(let i = 0; i < arr.length; ++i){
                            let data = arr[i];
                            data.imgUrl = data.imagUrl;
                            data.istmall = data.isTmall;
                            data.zk_money = data.coupon;
                            data.hasCoupon = true;
                            data.imageCombine = "";
                            delete data.imagUrl;
                            delete data.isTmall;
                            delete data.coupon;
                            arr[i] = cHome.toDataForMoments(data);
                        }
                        // list.dataArray = list.dataArray.concat(arr);
                        // list.dataArray = list.dataArray.concat(arr);
                        // arr = list.dataArray;
                        list.imageArray = arr.map(v=>v.imgUrl);
                    }
                    params.add(r.data.list);
                }
                else{
                    params.add();
                }
            }else{
                params.add();
            }
        });
    }
    onItem(idx, params){
        const index = params.index;
        const item = params.item;
        // ==
        if(idx != IDX_HOT){
            return <ViewMoments
                title={item.titile} 
                source={item.image} 
                date={item.createtime}
                onPress={this.onShare.bind(this,item)}
                content = {item.content}
                images={item.content_Images}
            />
        }
        return <ViewHotDaily
            title={item.titile} 
            source={item.image} 
            date={item.createtime}
            onShare={datas=>this.onShareCombine(item, datas)}
            content = {item.content}
            datas = {item.dataArray}
            images = {item.imageArray}
        />
    }
    // 分享
    onShare(item){
        let item2 = item;
        if(item.content.indexOf(item.titile) >= 0){
            item2 = { ...item };
            item2.content = item.content.substr(item.titile.length);
        }
        Navigator.push("ShareTo", {eshare:eShare.material, data:item2, images:item2.content_Images});
    }
    // 回去分享信息
    fetConver(data){
        if(data.qrcode){
            return Promise.resolve(data);
        }
        return new Promise((resolve, reject)=>{
            cHome.toConver(data).then(r=>{
                resolve({...data, ...r});
            }).catch(err=>{
                reject(err);
            })
        });
    }
    // 商品信息 合成图
    onShareCombine(item, datas){
        Dialog.retain(true);
        Promise.all(datas.map(v=>this.fetConver(v))).then(rs=>{
            Dialog.release();
            let item2 = item;
            if(item.content.indexOf(item.titile) >= 0){
                item2 = { ...item };
                item2.content = item.content.substr(item.titile.length);
            }
            // isCombine:true
            Navigator.push("ShareTo", {eshare:eShare.hotdaily, images:item.imageArray, data:item2, datas:rs});
        }).catch(err=>{
            Dialog.release();
        })
    }
    render(){
        return <View style = {{flex:1, backgroundColor:Color.back}}>
            <Nav.StateBar />
            <ProjectorLR fields = {["每日爆款","推广素材"]} barBorderStyle={{backgroundColor:Color.theme}}>
                <Flist
                    onLoad={params=>this.onLoad(0, params)}
                    onItem={params=>{return this.onItem(0, params);}}
                />
                <Flist
                    onLoad={params=>this.onLoad(1, params)}
                    onItem={params=>{return this.onItem(1, params);}}
                />
            </ProjectorLR>
        </View>;
    }
}